import copy

from docx.oxml import CT_Fonts
from openpyxl import load_workbook
from docx import Document
from docx.oxml.ns import qn
    # 加载Word模板
doc = Document(r"")




for paragraph in doc.paragraphs:
        # 深度复制段落内容，包括样式。如果不深度复制，样式会丢失
        list_runs1 = copy.deepcopy(paragraph.runs)
        list_runs2 = copy.deepcopy(paragraph.runs)
        paragraph.clear()
        #文字替换
        for i in range(len(list_runs1)):
            print(list_runs1[i].text)
            list_runs1[i].text = list_runs1[i].text.replace("CITY", "铜仁市")
            list_runs1[i].text = list_runs1[i].text.replace("YEAR", "2025")
            list_runs1[i].text = list_runs1[i].text.replace("TOTAL", "225")
            run = paragraph.add_run(text=f'{str(list_runs1[i].text)}')
            # 段落样式的复制
            # run.element.rPr.rFonts = list_runs2[i].element.rPr.rFonts
            run.font.name = str(list_runs2[i].font.name)
            run.element.rPr.rFonts.set(qn('w:eastAsia'), eval(str(list_runs2[i].element.rPr.rFonts.attrib))['{http://schemas.openxmlformats.org/wordprocessingml/2006/main}eastAsia'])
            run.font.size = list_runs2[i].font.size


doc.save(r"")



